<?php
/**
 * RUBIK IT Solution & Development ltd.
 *
 * PROJECT		: Rubik Ultimate
 * @version 	: 1.1
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * @author			hoadn <hoadn@rubik.com.vn>
 *
 * Created on	:	Nov 21, 2008
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class AbstractHtmlFieldInfo extends Object {
	var $_fieldName;
	var $_basicName;
	
	var $_hasTblOrder = true;

	var $_value			= null;
	var $_linkRule 		= null;
	
	var $isHtmlFieldInfo = true;
	
	var $_minLength = null;
	var $_maxLength = null;
	var $_validateCssClass = array();
	
	var $_showLabel = true;
	
	/**
	 * Constructor
	 *
	 * @param string $basicName
	 */
	function __construct($basicName, $linkRule = null) {
		$this->_fieldName 	= $basicName;
		$this->_basicName 	= $basicName;
		$this->_linkRule 	= $linkRule;
	}
	
	/**
	 * Set the valiadte informations
	 *
	 * @param string $validateCssClasses
	 * @param int $maxLength
	 * @param int $minLength
	 */
	function setValidates($validateCssClasses, $maxLength=null, $minLength = null) {
		$this->_validateCssClass['base'] = $validateCssClasses;
		$this->_maxLength 		 = $maxLength;
		$this->_minLength		 = $minLength;
	}
	
	/**
	 * Set Validate CSS classes
	 *
	 * @param string $validateCssClasses
	 * @param string $key
	 */
	function setValidateCssCls($validateCssClasses, $key='base') {
		$this->_validateCssClass[$key] = $validateCssClasses;
	}
	
	/**
	 * Set the valiadte informations
	 *
	 * @param int $maxLength
	 * @param int $minLength
	 */
	function setMinMaxLength($maxLength, $minLength = null) {
		$this->_maxLength = $maxLength;
		$this->_minLength = $minLength;
	}
	
	/**
	 * Show lable in the form or not.
	 * 
	 * @return boolean
	 */
	function showLabel() {
		return $this->_showLabel;
	}

	/**
	 * Set show label or not
	 *
	 * @param boolean $show
	 */
	function setShowLabel($show = true) {
		$this->_showLabel = $show;
	}
	
	/**
	 * Table has order or not
	 *
	 * @return boolean
	 */
	function hasTblOrder() {
		return $this->_hasTblOrder;
	}
	
	/**
	 * Setting for the rendered table has order or not
	 *
	 * @param boolean $hasTblOrder
	 */
	function setHasTblOrder($hasTblOrder) {
		$this->_hasTblOrder = $hasTblOrder;
	}
	
	/**
	 * Get basic name
	 *
	 * @return string
	 */
	function getBasicName() {
		return $this->_basicName;
	}
	
	/**
	 * Set Prefix for Mutilanguage
	 *
	 * @param str $prefix
	 */
	function setNamePrefix($prefix) {
		$this->_fieldName = $prefix . $this->_fieldName;
	}

	/**
	 * Set the value
	 *
	 */
	function setValue($valueOrArrayContain = null) {
		$this->_value = RubikHelper::getValueFrom($valueOrArrayContain, $this->_fieldName);
	}
	
	/**
	 * Get the value
	 *
	 * @return mixed
	 */
	function getValue() {
		return $this->_value;
	}

	/**
	 * Set the link rule for creating the view data
	 *
	 * @param string/Object $linkRule
	 */
	function setLinkRule($linkRule) {
		$this->_linkRule = $linkRule;
	}
	
	/**
	 * Function for geting the html for inputing data
	 *
	 * @abstract 
	 * @return string
	 */
	function getHtmlInputData($dataType = '', $extAttributes = '') {
		RubikHelper::printDebugCallStack();
		exit('The function:'.__FUNCTION__.' must be implement by the subclass');
	}
	
	/**
	 * Function for geting the html for previewing data
	 *
	 * @return string
	 */
	function getPreviewDataHtml($dataType = '', $key = '', $extAttributes = '', $borderTag = 'div') {
		$htmlData = $this->buildHtmlPreviewData($extAttributes, $key, $dataType);
		$rule = $this->_linkRule;
		if ($rule) {
			if (is_object ( $rule )) {
				$link = $rule->getLink () . "&$dbKeyField=" . $key;
			} else if (strpos ( $rule, '$key' ) !== false) {
				eval ( "\$link = \"{$rule}\";" );
			} else {
				$link = $rule . $key;
			}
			$htmlData = "<a href=\"$link\">$htmlData</a>";
		}
		
		if ($borderTag) {
			$htmlData = "<{$borderTag} class=\"{$dataType}_HtmlField\">{$htmlData}</{$borderTag}>";
		}
		
		return $htmlData;
	}
	
	/**
	 * Build the validate Html attribute for current control
	 *
	 * @param string $dataType (SYM_TYPE_EMAIL, SYM_TYPE_NUMBER,...)
	 * @return string
	 */
	function buildValidateHtmlAttribute($dataType = '', $extCls = '') {
		if ($dataType == SYM_TYPE_FLOAT) {
			$this->setValidateCssCls('number', $dataType);
		} else if ($dataType == SYM_TYPE_NUMBER) {
			$this->setValidateCssCls('digits', $dataType);
		} else if ($dataType == SYM_TYPE_EMAIL) {
			$this->setValidateCssCls('email', $dataType);
		}
		
		if ($extCls || count($this->_validateCssClass)) {
			$cls = $extCls. " ". implode(' ', $this->_validateCssClass);
			$cls = "class=\"{$cls}\"";
		}
		if ($this->_minLength) {
			$this->_minLength = intval($this->_minLength);
			$min = "minlength=\"{$this->_minLength}\"";
		}
		if ($this->_maxLength) {
			$this->_maxLength = intval($this->_maxLength);
			$max = "maxlength=\"{$this->_maxLength}\"";
		}
		
		return "{$cls} {$max} {$min}";
	}
	
	/**
	 * Build the html for previewing the data
	 * NOTE: please override in case special treat
	 * 
	 * @param string $extAttributes
	 * @param int $key key value
	 * @param string $dataType DB Field data type
	 * @return string
	 */
	function buildHtmlPreviewData($extAttributes = '', $key=null, $dataType ='S') {
		return $this->_value;
	}

	/**
	 * Function for geting the html for inputing data
	 * NOTE: please override in case special treat
	 * 
	 * @return string
	 */
	function getHtmlFilterData($dataType = '', $extAttributes = '') {
		return $this->getHtmlInputData($dataType, $extAttributes);
	}
	
	/**
	 * For checking if we need to create the filter header
	 * NOTE: please override in case special treat
	 *
	 * @return boolean
	 */
	function shouldCreateFilter() {
		return true;
	}
}
?>
